import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
} from 'vue-router'
import { getSsoUrl } from '@/api/sso'
import { useAuthStore } from '@/stores/auth'
import Home from '@/views/Home.vue'
import Container from '../components/container/src/index.vue'
const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      component: Container,
      children: [
        {
          path: '/',
          component: Home,
        },
        {
          path: '/chooseIcon',
          component: () => import('../views/chooseIcon/index.vue'),
        },
        {
          path: '/chooseArea',
          component: () => import('../views/chooseArea/index.vue'),
        },
        {
          path: '/trend',
          component: () => import('../views/trend/index.vue'),
        },
        {
          path: '/chooseTime',
          component: () => import('../views/chooseTime/index.vue'),
        },
        {
          path: '/notification',
          component: () => import('../views/notification/index.vue'),
        },
        {
          path: '/menu',
          component: () => import('../views/menu/index.vue'),
        },
        {
          path: '/chooseCity',
          component: () => import('../views/chooseCity/index.vue'),
        },
        {
          path: '/progress',
          component: () => import('../views/progress/index.vue'),
        },
        {
          path: '/calendar',
          component: () => import('../views/calendar/index.vue'),
        },
        {
          path: '/form',
          component: () => import('../views/form/index.vue'),
        },
        {
          path: '/modalForm',
          component: () => import('../views/modalForm/index.vue'),
        },
        {
          path: '/table',
          component: () => import('../views/table/index.vue'),
        },
        {
          path: '/map',
          children: [
            {
              path: '/map/AMap',
              component: () => import('../views/map/AMap/index.vue'),
            },
            {
              path: '/map/TMap',
              component: () => import('../views/map/TMap/index.vue'),
            },
            {
              path: '/map/BMap',
              component: () => import('../views/map/BMap/index.vue'),
            },
          ],
        },
        {
          path: '/echarts',
          component: () => import('../views/echarts/index.vue'),
        },
        {
          path: '/video',
          children: [
            {
              path: '/video/dahuatech',
              component: () => import('../views/video/dahua/index.vue'),
            },
            {
              path: '/video/hikvision',
              component: () => import('../views/video/haikang/index.vue'),
            },
          ],
        },
      ],
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        if (savedPosition) {
          resolve(savedPosition)
        } else {
          resolve({
            top: 0,
          })
        }
      }, 0)
    })
  },
})
const toLogin = async (message) => {
  if (message && message.indexOf('无效access_token') >= 0) {
    message = '无效的会话，或者会话已过期，请重新登录。'
  }
  getSsoUrl({
    url: location.href,
  }).then(({ data: { data: url } }) => {
    location.href = url
  })
}
const getUrl = () => {
  const url = new URL(window.location.href)
  const params = new URLSearchParams(url.search)

  // 要移除的参数列表
  const paramsToRemove = ['authorizationCode', 'accessToken', 'access_token']

  // 移除指定参数
  paramsToRemove.forEach((param) => params.delete(param))

  // 重新构建 URL
  const newUrl = `${url.origin}${url.pathname}${
    params.toString() ? '?' + params.toString() : ''
  }`
  return newUrl
}
// 全局路由守卫
router.beforeEach((to, from, next) => {
  next()
})
export default router
